package com.gbco.intelligentpush.utils;

import com.alibaba.fastjson.JSONObject;
import com.gbco.intelligentpush.conf.WxConfiguration;
import com.gbco.intelligentpush.entity.menuentity.Button;
import com.gbco.intelligentpush.entity.menuentity.Menu;
import com.gbco.intelligentpush.entity.menuentity.ViewButton;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * @ Author     ：tk.
 * @ Date       ：Created in 09:51 2020/05/23
 * @ Description：微信工具类
 * @ Modified By：
 * @Version: 0.0.1$
 */

@Slf4j
@Component
public class WxUtil {

    @Autowired
    WxConfiguration wxConfiguration;

    private static String TOKEN;

    private static String GET_ACCESSTOKEN_URL;

    private static String APPID;

    private static String APPSECRET;

    private static String domain;

    private static String btn_1;

    private static String btn_1_url;

    private static String GET_JSAPI_TICKET_URL;

    public WxUtil(WxConfiguration wxConfiguration) {
        TOKEN = wxConfiguration.getTOKEN();
        GET_ACCESSTOKEN_URL = wxConfiguration.getGET_ACCESSTOKEN_URL();
        APPID = wxConfiguration.getAPPID();
        APPSECRET = wxConfiguration.getAPPSECRET();
        domain = wxConfiguration.getDomain();
        btn_1 = wxConfiguration.getBtn_1();
        btn_1_url = wxConfiguration.getBtn_1_url();
        GET_JSAPI_TICKET_URL = wxConfiguration.getGET_JSAPI_TICKET_URL();
    }

    //拿取JSApi_ticket 参数为全局token
    public static String getJSAPITICKET(String ACCESSTOKEN){

        return HttpUtil.get(GET_JSAPI_TICKET_URL.replaceAll("ACCESS_TOKEN",ACCESSTOKEN));
    }

    //token验证
    public static boolean check(String timestamp, String nonce, String signature) {

        // 将token、timeStamp、nonce三个参数进行字典序排序
        String[] strs = new String[] { TOKEN, timestamp, nonce };
        Arrays.sort(strs);
        // 将三个参数字符串拼接成一个字符串
        String str = strs[0] + strs[1] + strs[2];
        // 进行sha1加密
        String jiami = sha1(str);

        // 开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
        return jiami.equals(signature);
    }

    //sha1加密
    private static String sha1(String str) {
        try {
            // 获取一个加密对象
            MessageDigest md = MessageDigest.getInstance("sha1");
            // 加密
            byte[] digest = md.digest(str.getBytes());
            char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            StringBuffer sb = new StringBuffer();
            // 处理加密结果
            for (byte b : digest) {
                sb.append(chars[(b >> 4) & 15]);
                sb.append(chars[b & 15]);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

   //向微信服务器获取Access_token
    public static String getAccesstoken(){
        log.info("发送请求获取Accesstoken");

        return HttpUtil.get(GET_ACCESSTOKEN_URL.replaceAll("APPID",APPID).replaceAll("APPSECRET",APPSECRET));
    }


    //生成菜单json
    public static String getMenuJSON(){

        //诊断链接拼接
        String url = btn_1.replaceAll("APPID",APPID).replaceAll("REDIRECT_URI",domain+btn_1_url);

        Menu menu ;
        Button button1,button2;
        //1级按钮
        button1 = new ViewButton("在线诊断","view",url);
        button2 = new ViewButton("实时报告","view","https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_pc_3");

        //菜单
        menu = new Menu(new Button[]{button1,button2});
        return JSONObject.toJSONString(menu);
    }

}
